package com.czn.shopdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czn.shopdemo.mapper.CategoryMapper;
import com.czn.shopdemo.mapper.GoodsMapper;
import com.czn.shopdemo.model.Category;
import com.czn.shopdemo.model.Category;
import com.czn.shopdemo.model.Goods;
import com.czn.shopdemo.model.enums.ResultCodeEnum;
import com.czn.shopdemo.model.exception.CustomException;
import com.czn.shopdemo.service.CategoryService;
import com.czn.shopdemo.service.GoodsService;
import com.czn.shopdemo.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Zenan Chen
 * @date 2023/4/3 15:15
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private GoodsMapper goodsMapper;


    /**
     * 保存商品标签标签信息（新增和修改）
     *
     * @param category
     */
    @Override
    public void saveCategory(Category category) {
        // 新增
        if (Utils.isEmptyStr(category.getUuid())) {
            // 先校验code是否已经存在
            QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("code", category.getCode());
            Category categoryExist = categoryMapper.selectOne(queryWrapper);
            if (categoryExist != null) {
                throw new CustomException(ResultCodeEnum.CODE_EXIST);
            }
            category.setUuid(UUID.randomUUID().toString().replaceAll("-", ""));
            category.setCreateTime(new Date());
            category.setUpdateTime(new Date());
            categoryMapper.insert(category);
            //更新
        } else {
            // 如果该标签已被使用，则不能修改
            checkCategoryUsed(category.getUuid());
            category.setUpdateTime(new Date());
            categoryMapper.updateById(category);
        }
    }

    /**
     * 删除商品标签
     *
     * @param uuid
     */
    @Override
    public void deleteCategory(String uuid) {
        checkCategoryUsed(uuid);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("uuid", uuid);
        categoryMapper.delete(wrapper);
    }

    /**
     * 检测标签被商品的使用情况
     *
     * @param uuid
     */
    private void checkCategoryUsed(String uuid) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", uuid);
        List<Goods> goods = goodsMapper.selectList(queryWrapper);
        if (goods != null && goods.size() > 0) {
            throw new CustomException("该商品标签已被挂载使用，请先卸载再删除！", 211);
        }
    }


    /**
     * 获取商品标签详情信息
     *
     * @param uuid
     * @return
     */
    @Override
    public Category getCategoryDetail(String uuid) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid", uuid);
        Category category = categoryMapper.selectOne(queryWrapper);
        return category;
    }

    /**
     * 获取商品标签列表
     *
     * @return
     */
    @Override
    public List<Category> getCategoryList() {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", "0");
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        return categories;
    }
}
